package com.umeox.watch.moto.dataservice.utils;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.Settings;

import com.umeox.moto.common.executor.ThreadExecutor;
import com.umeox.moto.common.log.Logger;
import com.umeox.moto.common.utils.FileUtils;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.dataservice.base.BaseApp;
import com.umeox.watch.moto.dataservice.constants.Constants;
import com.umeox.watch.moto.dataservice.db.AppDataBase;
import com.umeox.watch.moto.dataservice.services.ApiService;

/**
 * Created by Rambo 2019-12-14
 * Describe:
 */
public final class PushReceiverHelper {

    private volatile static PushReceiverHelper instance;

    private PushReceiverHelper() {

    }

    public static PushReceiverHelper getInstance() {
        if (instance == null) {
            synchronized (PushReceiverHelper.class) {
                if (instance == null) {
                    instance = new PushReceiverHelper();
                }
            }
        }
        return instance;
    }

    @SuppressLint("WrongConstant")
    public void unbind() {
        //清除经纬度
        Settings.Global.putString(BaseApp.getInstance().getContentResolver(), "LAT", "-999");
        Settings.Global.putString(BaseApp.getInstance().getContentResolver(), "LONG", "-999");

        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_RECEIVER_FOREGROUND | Constants.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        intent.setAction(Constants.ACTION_UNBIND);
        BaseApp.getInstance().sendBroadcast(intent);
        Logger.d("设备已解绑");


        ThreadExecutor.getInstance().io().execute(() -> {
            //删除联系人&好友头像
            FileUtils.deleteFile(FileUtils.getImageCaheDir());
            FileUtils.deleteFile(FileUtils.getAppGlideCache());
        });

        BaseApp.getInstance().getMmkv().clearAll();
        deletePhonebook();

        try {
            Logger.d("删除联系人：" + AppDataBase.getInstance().contactDao().deleteAll());
            Logger.d("删除好友：" + AppDataBase.getInstance().buddyDao().deleteAll());
            Logger.d("删除闹钟：" + AppDataBase.getInstance().alarmDao().deleteAll());
            Logger.d("删除壁纸：" + AppDataBase.getInstance().wallpaperDao().deleteAll());
            Logger.d("删除记步设置：" + AppDataBase.getInstance().stepInfoDao().deleteAll());
            Logger.d("删除定位频率设置：" + AppDataBase.getInstance().locationPeriodDao().deleteAll());
            Logger.d("删除禁用时段设置：" + AppDataBase.getInstance().forbidPeriodDao().deleteAll());
            Logger.d("删除位置信息：" + AppDataBase.getInstance().locationInfoDao().deleteAll());
            Logger.d("删除HolderInfo：" + AppDataBase.getInstance().holderInfoDao().deleteAll());
            Logger.d("删除视频通话记录：" + DataProvider.removeVideoCalllog());
            Logger.d("删除Chat联系人：" + DataProvider.removeAllChatContact());
            Logger.d("删除运动挑战数据：" + DataProvider.deleteAllSportsChallenge());
            Logger.d("删除记步信息：" + DataProvider.removePedometer());
            Logger.d("重置所有上传计步记录：" + DataProvider.resetAllUploadFlag());
            Logger.d("删除任务信息：" + AppDataBase.getInstance().tasksAttributeDao().deleteAll());
        } catch (Exception exception) {
            exception.printStackTrace();
        }


        //通知闹钟应用取消所有已设置的闹钟
        BaseApp.getInstance().sendBroadcast(new Intent().addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_RECEIVER_FOREGROUND)
                .setAction(Constants.ACTION_CANCEL_ALARM));
        //通知运动挑战应用取消所有已设置的闹钟
        BaseApp.getInstance().sendBroadcast(new Intent().addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_RECEIVER_FOREGROUND)
                .setAction("com.umeox.moto.intent.action.CANCEL_ALARM"));
        //通知祈祷时间应用取消所有已设置的闹钟
        BaseApp.getInstance().sendBroadcast(new Intent().addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_RECEIVER_FOREGROUND)
                .setAction("com.wherecom.intent.action.CANCEL_PRAYER_ALARM"));
    }

    /**
     * 清空电话簿联系人
     */
    private void deletePhonebook() {
        Uri uri = Uri.parse(ContactsContract.RawContacts.CONTENT_URI.toString() + "?" + ContactsContract.CALLER_IS_SYNCADAPTER + "=true");
        BaseApp.getInstance().getApplicationContext().getContentResolver().delete(uri, "_id!=-1", null);
        uri = Uri.parse(ContactsContract.Data.CONTENT_URI.toString() + "?" + ContactsContract.CALLER_IS_SYNCADAPTER + "=true");
        BaseApp.getInstance().getApplicationContext().getContentResolver().delete(uri, "_id!=-1", null);
        uri = Uri.parse(ContactsContract.Groups.CONTENT_URI.toString() + "?" + ContactsContract.CALLER_IS_SYNCADAPTER + "=true");
        BaseApp.getInstance().getApplicationContext().getContentResolver().delete(uri, "_id!=-1", null);
    }


    public void bind() {
        ApiService.getDeviceConfig(BaseApp.getInstance().deviceId, (success, result) -> {
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_RECEIVER_FOREGROUND);
            intent.setAction(Constants.ACTION_ACTIVATION);
            BaseApp.getInstance().sendBroadcast(intent);

            Logger.d("设备已绑定");
        });
    }


}
